// 课件包是内容
import { request } from '@/api/request';
import { getOrgEntType } from '@/components/EntTypeSelect/utils';
import { getTradeGroupString, tradeGroupResult } from '@/components/Trade/utils';
import { ShelfMethods, ShelfProducts, UpperParams } from '../interface';
import Shelf from '../Shelf';

/**
 * @desc 未上架的课件包返回结果
 */
interface UnShelfPackage {
  packageId: string;
  packageName: string;
  ctDate: string;
  num: number;
  upDate: string;
  parClassId: string;
  classId: string;
  faceImg: string;
  aiLearnClassification?: number;
}

/**
 * @desc 已经上架的课件包的返回结果
 */
export type OnShelfPackage = {
  guid: string;
  region: string;
  entTypeCode: string;
  trade: string;
  isCharge: number;
  regionName: string;
  name: string;
  classId: string;
  creatTime: string;
  faceImg: string;
  tradeName: string;
  showTrade: string;
  aiLearnClassification?: number;
};
/**
 * @desc 自建资源库课件包的返回结果
 */
export type ShelfPackage = {
  resourceId: string;
  className: string;
  coursewareNum: number;
  createTime: string;
  isCharge?: number;
  resourceName: string;
};
// /**
//  * 课件包兼容字段
//  */
// interface PackageSelf {
//   /**
//    * @desc 数量
//    */
//   num: number;

//   /**
//    * @desc 更新时间
//    */
//   updateTime: string;
// }

class Package implements ShelfProducts, ShelfMethods {
  isOnShelf: boolean;
  name: string;
  guid: string;
  origin: Partial<UnShelfPackage & OnShelfPackage & ShelfPackage>;
  /**
   * 企业类型解析结果
   */
  entTypeCodeText?: string;
  /**
   * @desc 是否收费
   */
  isChargeText?: string;
  // tradeGroup?: tradeGroupResult[];
  showTradeArry?: tradeGroupResult[];
  aiLearnClassification?: number;

  constructor(guid: string, name: string, isOnShelf: boolean, origin: Partial<UnShelfPackage & OnShelfPackage>) {
    this.guid = guid;
    this.name = name;
    this.isOnShelf = isOnShelf;
    this.origin = origin;
    // 自己计算得到的
    this.entTypeCodeText = getOrgEntType(origin.entTypeCode);
    this.isChargeText = ['否', '是'][origin.isCharge ?? -1];
    // this.tradeGroup = getTradeGroupString(origin.showTrade);
    this.showTradeArry = getTradeGroupString(origin.showTrade);
    this.aiLearnClassification = origin.aiLearnClassification;
  }

  get num() {
    return this.origin.num ?? this.origin.coursewareNum;
  }

  /**
   * @desc 创建时间/发布时间
   */
  get createTime() {
    return this.origin.ctDate ?? this.origin.creatTime ?? this.origin.createTime;
  }

  get updateTime() {
    return this.origin['ctDate'];
  }

  /**
   * @desc 分类子级id
   */
  get classId() {
    return this.origin.classId;
  }

  /**
   * @desc 分类父级id
   */
  get parClassId() {
    return this.origin.parClassId;
  }

  /**
   * @desc 封面图
   */
  get cover() {
    return this.origin.faceImg;
  }

  /**
   * @desc 区域
   */
  get area() {
    return this.origin.regionName;
  }

  /**
   * @desc 行业
   */
  get trade() {
    return this.origin.tradeName;
  }

  static shelf = new Shelf(1);

  upper(params: Partial<UpperParams>): Promise<boolean> {
    return Package.shelf.upper(params);
  }
  down(guid: string): Promise<any> {
    return Package.shelf.down(guid);
  }

  /**
   * @desc 未发布列表
   */
  static async unShelfList(pageNum: number, pageSize: number, params: Record<string, unknown>) {
    const { data } = await request<{ list: UnShelfPackage[]; total: number }>({
      url: '/safe/api/industry/package/list',
      params: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => new Package(v.packageId, v.packageName, false, v)),
      total: data.total,
    };
  }

  /**
   * @desc 已发布列表
   */
  static async onShelfList(pageNum: number, pageSize: number, params: Record<string, unknown>) {
    const { data } = await request<{ list: OnShelfPackage[]; total: number }>({
      url: '/safe/api/shelves/packageList',
      data: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => new Package(v.guid, v.name, true, v)),
      total: data.total,
    };
  }

  /**
   * @desc 自建资源库-课件包列表
   */
  static async onPackageList(pageNum: number, pageSize: number, params: Record<string, unknown>) {
    const { data } = await request<{ list: ShelfPackage[]; total: number }>({
      url: '/safe/api/entCourseware/governmentPackageList',
      params: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => new Package(v.resourceId, v.resourceName, true, v)),
      total: data.total,
    };
  }

  /**
   * @desc boss，创建课件包,如果课件包已经存在则编辑课件包
   */
  static async create(params: { packageId?: string; [key: string]: unknown }) {
    await request({
      url: `/safe/api/industry/package/${params.packageId ? 'update' : 'create'}`,
      data: params,
    });
    return true;
  }
  /**
   * @desc 企业政协，创建课件包,如果有guid则编辑课件包
   */
  static async selfCreate(params: { guid?: string; [key: string]: unknown }) {
    await request({
      url: '/safe/api/entCourseware/insertGovAssembleCoursewarePackage',
      data: params,
    });
    return true;
  }
  /**
   * @desc 删除课件包
   */
  static async delete(packageId: string) {
    await request({
      url: '/safe/api/industry/package/delete',
      params: { packageId },
    });
    return true;
  }
  /**
   * @desc 企业协会删除课件包
   */
  static async entDelete(guid: string) {
    await request({
      url: '/safe/api/entCourseware/DelEntCoursewarePackage',
      params: { guid },
    });
    return true;
  }
}

export default Package;
